//
//  MainRecommendVC.m
//  ImGuider
//
//  Created by llt on 2017/7/2.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "MainRecommendVC.h"
#import <WebKit/WebKit.h>
#import "FileManager.h"

@interface MainRecommendVC () <WKNavigationDelegate, WKUIDelegate, WKScriptMessageHandler>

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation MainRecommendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"choosecitybg"]];
    bgImageView.contentMode = UIViewContentModeScaleToFill;
    bgImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view addSubview:bgImageView];
    
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    [config.userContentController addScriptMessageHandler:self name:@"imguider"];
    
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT) configuration:config];
    [self.webView setBackgroundColor:[UIColor clearColor]];
    [self.webView setOpaque:NO];
    self.webView.scrollView.scrollEnabled = NO;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:MYLocalizedString(@"START_H5", nil) ofType:nil];
    //中文路径要转码
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    if(path){
        
        if (kiOS9Later) {
            
            NSURL *fileURL = [NSURL fileURLWithPath:path];
            [self.webView loadFileURL:fileURL allowingReadAccessToURL:fileURL];
        } else {
            NSURL *fileURL = [FileManager fileURLForBuggyWKWebView8:[NSURL fileURLWithPath:path]];
            NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
            [self.webView loadRequest:request];
        }
    }
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    
    [self.view addSubview:self.webView];
}

- (void)selectCity:(NSString *)cityID {
    
    if ([_delegate respondsToSelector:@selector(selectCityID:)]) {
        [_delegate selectCityID:cityID];
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
    NSString *cityID = [message.body stringValue];
    
    [self selectCity:cityID];
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
    
    // 禁用选中效果
    [self.webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none'" completionHandler:nil];
    [self.webView evaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none'" completionHandler:nil];
    
}

@end


//#import "MainRecommendVC.h"
//#import <JavaScriptCore/JavaScriptCore.h>
//
//@protocol JSObjcDelegate <JSExport>
////tianbai对象调用的JavaScript方法，必须声明！！！
//- (void)selectCity:(NSString *)cityID;
//
//@end
//
//@interface MainRecommendVC ()<UIWebViewDelegate, JSObjcDelegate>
//
//@property (nonatomic, strong) UIWebView *webView;
//@property (nonatomic, strong) JSContext *jsContext;
//
//@end
//
//@implementation MainRecommendVC
//
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    [self setupUI];
//}
//
//- (void)setupUI {
//    
//    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"START_JPG", nil)]];
//    bgImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//    [self.view addSubview:bgImageView];
//    
//    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT)];
//    [self.webView setBackgroundColor:[UIColor clearColor]];
//    [self.webView setOpaque:NO];
//    self.webView.scrollView.scrollEnabled = NO;
//    
//    NSString *path = [[NSBundle mainBundle] pathForResource:MYLocalizedString(@"START_H5", nil) ofType:nil];
//    //中文路径要转码
//    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    
//    //加载html
//    NSURL *url = [NSURL URLWithString:path];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    [self.webView loadRequest:request];
//    
//    self.webView.delegate = self;
//    self.webView.scalesPageToFit = YES;
//    [self.view addSubview:self.webView];
//}
//
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}
//
//- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    
//    // 设置javaScriptContext上下文
//    self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//    //将tianbai对象指向自身
//    self.jsContext[@"imguider"] = self;
//    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
//        context.exception = exceptionValue;
//        IGLog(@"异常信息：%@", exceptionValue);
//    };
//}
//
//- (void)selectCity:(NSString *)cityID {
//    
//    dispatch_async(dispatch_get_main_queue(), ^ {
//        
//        if ([_delegate respondsToSelector:@selector(selectCityID:)]) {
//            [_delegate selectCityID:cityID];
//        }
//        
//        [self dismissViewControllerAnimated:YES completion:nil];
//    });
//}
//
//@end

